﻿// Define the entry point
$(document).ready(function () {
    // The DOM (document object model) is constructed
    // We will initialize and run our plugin here
    ExecuteOrDelayUntilScriptLoaded(screenshotPreview, "sp.js");
});


this.screenshotPreview = function () {

    var ctx = new SP.ClientContext.get_current();
    var webUrl = ctx.get_url();

    if (webUrl != "/")
        webUrl += "/";


    /* CONFIG */

    // these 2 variable determine popup's distance from the cursor
    // you might want to adjust to get the right result

    xOffset = 10;
    yOffset = 30;

    $("div.ms-vb.itx a").hover(function (e) {
        if (!$(this).attr('href').match(/^.+\.((doc)|(docx)|(ppt)|(pptx)|(xps)|(aspx)|(pdf)|(xlsx)|(dwg)|(wmv)|(png)|(gif)|(jpg)|(jpeg)|(zip))(\?+.*)?$/))
            return;
        this.t = this.title;
        this.title = "";
        var c = (this.t != "") ? "<br/>" + this.t : "";
        var strDoc = encodeURIComponent($(this).attr('href'));
        $("body").append("<div id='screenshot'><div id='transition'><img id='FirstImage' src=\"" + webUrl + "_layouts/SPThumbnailExtender/MobileGenericHandler.ashx?w=320&h=240&usecache=true&doc=" + strDoc + "&n=0\" alt=" + c + "/></div></div>");

        $.ajax({
            type: "GET",
            url: webUrl + "_layouts/SPThumbnailExtender/MobileGenericHandler.ashx?usecache=true&doc=" + strDoc + "&c=true",
            success: function (msg) {
                for (var miind = 1; miind < eval(msg); miind++) {
                    $("#transition").append("<img style=\"{width:'100%', height:'100%'}\" src=\"" + webUrl + "_layouts/SPThumbnailExtender/MobileGenericHandler.ashx?w=320&h=240&usecache=true&doc=" + strDoc + "&n=" + miind + "\" alt=" + c + " />");
                    if (miind > 10)
                        break;
                }
                $('#transition').jqFancyTransitions(
                {
                    width: $("#FirstImage").width(),
                    height: $("#FirstImage").height()
                }

                );

            }
        });

        $("#screenshot")
                        .css("top", (e.pageY - xOffset) + "px")
                        .css("left", (e.pageX + yOffset) + "px")
                        .css("background", "white url('/_layouts/images/GEARS_AN.gif') no-repeat center")
                        .css("min-height", "75px")
                        .fadeIn("fast");

    },
        function () {
            this.title = this.t;
            $("#screenshot").remove();
        });
    $("a.screenshot").mousemove(function (e) {
        $("#screenshot")
                        .css("top", (e.pageY - xOffset) + "px")
                        .css("left", (e.pageX + yOffset) + "px");
    });
}